<?php

require_once(WSB_MODULES_PATH .'sherpa/forms/GuestCheckoutEmail.php');

class Sherpa_GuestCheckoutLoginController extends WSB_Xtension_Extension
{

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	/**
	 * Initialisierung
	 */
	public function init()
	{
		$this->initTranslate();
	}

	/**
	 * Index Action
	 */
	public function indexAction()
	{
		$session = new Zend_Session_Namespace('Sherpa_Checkout');
		$request = $this->getRequest();
		$form = new Sherpa_Form_GuestCheckoutEmail();
		$defaults = array_merge(array('baEmail' => empty($session->baEmail) ? '' : $session->baEmail), $request->getPost());
		$form->setDefaults($defaults);

		$buttonClicked = ($request->getPost('btnGuestCheckout') != '') // Button
		 || ($request->getPost('btnGuestCheckout_x') != '') // Image button
		 || ($request->getPost('btnGuestCheckout_y') != '');  // Image button

		if($request->isPost() && $buttonClicked && $form->isValid($request->getPost())) {
			$session->baEmail = $form->getValue('baEmail');
			$this->_redirectToCheckout();
		} else {
			$form->setView($this->_view);
			$this->assignToView('form', $form);
			$this->renderView('guestcheckoutlogin/index.phtml');
		}

		// Eingeloggt
		$auth = WSB_Auth_Frontend::getInstance();
		if($auth->hasIdentity()) {
			$this->_redirectToCheckout();
		}

	}

	/**
	 * Redirect zur Checkout Seite
	 */
	private function _redirectToCheckout()
	{
		$pageId = $this->getConfiguration()->pageIdCheckout;
		$url = WSB_Xtension_Url::getInstance()->getHrefToPage($pageId);
		$this->redirect($url);
	}

}